<style src="./index.scss" lang="scss" scoped rel="stylesheet/scss"></style>
<template src="./index.html"></template>
<script>
export default {
    mixins: [Vue.prototype.$vFN.Mixins.Base, Vue.prototype.$vFN.Mixins.List],

    props: {
        // 使用者window实例 必定义
        appWindow: {
            type: Object,
            default: null,
        },
        // 接口列表
        apiConfig: {
            type: Object,
            default: {
                // 分组列表
                albumList: {
                    url: "mall-site-portal/album/listAll",
                },
                // 分组添加
                albumAdd: {
                    url: "mall-site-portal/album/create",
                },
                // 分组删除
                albumDel: {
                    url: "mall-site-portal/album/delete",
                },
                // 分组编辑
                albumEdit: {
                    url: "mall-site-portal/album/update",
                },
                // 数据列表
                dataList: {
                    url: "mall-site-portal/album-info/list",
                },
                // 数据删除
                dataDel: {
                    url: "mall-site-portal/album-info/delete",
                },
                // 数据修改
                dataEdit: {
                    url: "mall-site-portal/album-info/update",
                },
                // 数据同步
                dataSync: {
                    url: "mall-site-portal/album-info/materialUpload",
                },
            },
        },
        // 模式  如果为wx 则为微信资源
        mode: {
            type: String,
            default: "",
        },
        // 是否选择模式
        isSelect: {
            type: Boolean,
            default: false,
        },
        // 最大上传数量
        count: {
            type: Number,
            default: -1,
        },
    },
    components: {
        AudioListCheck: async () =>
            Vue.extend(
                await Vue.prototype.$vFN.Components.Base.AudioListCheck()
            ),
        MaterialGroup: async () =>
            Vue.extend(
                await Vue.prototype.$vFN.Components.Base.MaterialGroup()
            ),
    },
    data() {
        return {
            selectList: [],

            // 分组
            albumList: [],
            // 页面接口地址
            apiUrl: null,

            // 表格查询数据
            mixinList_tableSearchData: {
                albumId: "",
                // 是否微信素材 null全部 0否 1是
                isWechat: null,
                name: "",
                // 排序  name 名称 , created_time 上传时间 updated_time 更新时间
                orderBy: "created_time",
            },
            // 列表常量查询参数
            mixinList_tableSearchConst: {
                // 分类 1图片2视频3音频
                type: 3,
            },

            // 表格数据
            tableData: [],

            editVisible: false,
        };
    },
    methods: {
        // 扩展方法，
        mixinList_getTableExtend(data_) {
            if (!this.isSelect) {
                this.selectList = [];
            }
            return data_;
        },
        // 修改素材资源
        editBoxOpen(item) {
            this.$vFN.Directives.UpdateResources({
                appWindow: this.appWindow,
                albumId: item.albumId,
                id: item.id,
                name: item.name,
                type: this.mixinList_tableSearchConst.type,
                apiConfig: this.apiConfig,
                onSuccess: () => {
                    this.$refs.materialGroupRef.refresh();
                },
            });
        },

        // 全选
        handleCheckAllChange(e) {
            if (e) {
                let _arr = [];
                this.tableData.forEach((item) => {
                    _arr.push(item.albumUrl);
                });
                this.selectList = _arr;
            } else {
                this.selectList = [];
            }
        },
        // 判断是否全选
        isAll() {
            if (this.selectList.length < this.tableData.length) {
                return false;
            } else {
                return true;
            }
        },
        // 分组回调
        materialChange(r) {
            if (r) {
                this.mixinList_tableSearchData.albumId = r;
            }
            if (this.mixinList_tableSearchData.albumId) {
                this.mixinList_refreshData();
            } else {
                this.mixinList_loading = false;
            }
        },
        // 上传图片
        uploadRes() {
            this.$vFN.Directives.UploadResourcesAudio({
                appWindow: this.appWindow,
                albumId: this.mixinList_tableSearchData.albumId,
                apiConfig: this.apiConfig,
                onSuccess: () => {
                    this.$refs.materialGroupRef.refresh();
                },
            });
        },

        // 选择完成
        submit() {
            this.onSuccess({
                data: [...this.selectList],
            });
            this.editBoxCancel();
        },
        // 调用组件内方法获取对象集合
        getObj() {
            return this.$refs.audioListCheckRef.getObj();
        },
        // 获取需要提交的ids
        getIds(item_) {
            // 生成ids
            let _arr = [];
            if (item_) {
                _arr.push(item_.id);
            } else {
                _arr = this.getObj().ids;
            }
            return _arr;
        },
        // 选中删除
        clickDel(item_) {
            this.$confirm("确定删除选中内容么", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                dangerouslyUseHTMLString: true,
            })
                .then(async () => {
                    this.delSubmit(this.getIds(item_));
                })
                .catch(() => {});
        },
        // 删除提交
        async delSubmit(_arr) {
            let _data = await this.$vFN.Http.post(
                {
                    url: this.apiConfig.dataDel.url,
                    domain: this.apiConfig.dataDel.domain,
                    data: {
                        ids: _arr,
                    },
                },
                this.appWindow
            );

            if (_data._ok) {
                this.$vFN.Tools.messageSuccess("操作成功");
                this.$refs.materialGroupRef.refresh();
            }
        },
        // 选中同步至微信
        clickSyncWx(item_) {
            this.$confirm("确定同步选中内容至微信么", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                dangerouslyUseHTMLString: true,
            })
                .then(async () => {
                    this.syncWxSubmit(this.getIds(item_));
                })
                .catch(() => {});
        },
        // 同步微信提交
        async syncWxSubmit(_arr) {
            this.mixinList_loading = true;
            let _data = await this.$vFN.Http.post(
                {
                    url: this.apiConfig.dataSync.url,
                    domain: this.apiConfig.dataSync.domain,
                    data: {
                        albumInfoIdLst: _arr,
                    },
                },
                this.appWindow
            );

            if (_data._ok) {
                this.$vFN.Tools.messageSuccess("操作成功");
                this.$refs.materialGroupRef.refresh();
            }
        },
    },
    watch: {},
    mounted() {
        this.albumApi = {
            list: {
                url: this.apiConfig.albumList.url,
                domain: this.apiConfig.albumList.domain,
            },
            add: {
                url: this.apiConfig.albumAdd.url,
                domain: this.apiConfig.albumAdd.domain,
            },
            del: {
                url: this.apiConfig.albumDel.url,
                domain: this.apiConfig.albumDel.domain,
            },
            edit: {
                url: this.apiConfig.albumEdit.url,
                domain: this.apiConfig.albumEdit.domain,
            },
        };
        this.apiUrl = {
            list: {
                url: this.apiConfig.dataList.url,
                domain: this.apiConfig.dataList.domain,
            },
        };
    },
    beforeCreate() {},
};
</script>
